<?php
namespace app\index\home;
use app\index\home\Base;
use think\Db;
use Env;
class Qrcode extends Base
{
    protected function initialize()
    {
       parent::initialize();
       
    }
    function test(){
        $access_token = $this->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/express/business/account/getall?access_token=".$access_token;
        $url2 = "https://api.weixin.qq.com/cgi-bin/express/business/delivery/getall?access_token=".$access_token;
        $rs = file_get_contents($url2);
        $wuliu_list = json_decode($rs,true);
        var_dump($wuliu_list['data']);exit;
    }

    function getQrcodeApid(){
        $path = input('path/s');
        $scene = input('scene/s');
        $pid = input('product_id/d');
        $openid = input('openid/s');
        $uid = $this->getUserId($openid);

        $qrcode_check = Db::name('product_qrcode')->where(array('uid'=>$uid,'pid'=>$pid))->value('qrcode');
        if($qrcode_check){
            okMsg(array('qrcode'=>$qrcode_check));
        }

        $scene .= "&pid=".$uid;
        $rs = $this->getQrcode($path,$scene);
        
        if(strlen($rs)<1000){
            $rs = json_decode($rs,true);
            errMsg($rs['errmsg']);
        }
        if($rs){
            $file_path = 'upload/qrcode/qrcode_'.$pid.time().'.png';
			$file = fopen($file_path,'w');
			fwrite($file,$rs);
			fclose($file);

            $in_data = array(
                'uid'=>$uid,
                'pid'=>$pid,
                'qrcode'=>$file_path,
                'ctime'=>time()
            );
            $res = Db::name('product_qrcode')->insert($in_data);
            if($res){
                okMsg(array('qrcode'=>$file_path));
            }
            else{
                errMsg('获取失败，请重试!');
            }
            
        }
        else{
            errMsg('获取失败，请重试');
        }
    }

    function getQrcode($path,$scene,$version='trial',$touming=false){
        $access_token = $this->getAccessToken();
        // echo $access_token;exit;
		$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token;
		
		$data = array(
			"page"=>$path,
			"check_path"=>false,
			"scene"=>$scene,
			"env_version"=>$version,//要打开的小程序版本。正式版为 "release"，体验版为 "trial"，开发版为 "develop"
			"is_hyaline"=>$touming
		);
        // var_dump($data);exit;
        $rs = juhecurl($url,json_encode($data),1);
        return $rs;
    }

    //获取
	function getAccessToken(){
        $token_info = Db::name('access_token')->where('id',1)->find();
        if($token_info['access_token']&&time()-$token_info['ctime']<7000){
            return $token_info['access_token'];
        }
        else
        {	
            $appid=config('APPID');
            $appserct=config('APPSECRET');
            if(empty($appid)||empty($appserct)){
                return false;
            }
            $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appserct}";
            $getjson=file_get_contents($url);
            $getdata=json_decode($getjson,true);
            $rt=$getdata['access_token'];
            $time = time();
            if(!$token_info){
                $rs = Db::name('access_token')->insert(array('access_token'=>$rt,'ctime'=>$time));
            }
            else{
                $up_data['access_token'] = $rt;
                $up_data['ctime'] = $time;
                $rs = Db::name('access_token')->where('id',1)->update($up_data);
            }
            if($rs){
                return $rt;
            }
            else{
                return false;
            }
            
        }
      
    }

    function getUserId($openid){
        // $wxOauth = new Wxoauth();
        // $id = $wxOauth->getUserId();
        // if(!$id){
        //     noLogin();
        // }
        if(!$openid){
            noLogin();
        }
        $id = Db::name('user')->where('openid',$openid)->value('id');
        return $id;
    }

    function bindUser(){
        $access_token = $this->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/express/business/printer/update?access_token=".$access_token;
        $arr[] = 'otGf-5fOaVUPYaqok7r4YIjQ-EHw';
        foreach($arr as &$value){
            $data['openid'] = $value;
            $data['update_type'] = 'bind';
            $rs = juhecurl($url,json_encode($data),1);
            var_dump($rs);
        }

       
    }

    function getKfLink(){
        // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww9eb6411c0b6a63e6&corpsecret=07HO-5qdJ7PrD2Li-kj6MxkVnN65vf3WykBkDaaVHZY";
        // $access_token = $this->getAccessToken();

        $access_token = "7zhEYFJdMPaTgssVoAx2FW-qcDUbYLUKynJimQAZ2K-_pkRzYCMOSOz7xfA3MKoxuawSPnEGVVqLKwp7Q9SPqOOol09ZEvLZHyJMC0Um_ZnYEloO1onRVXBeL28dICFCef06tvxok8BLK4jNyfLo8G_uombZxWJUc4FzZePi69FhYoVMkZHKZYxeRcfmFUnldALoeLPuwLrLVqXp1-k4xA";
        $url = "https://qyapi.weixin.qq.com/cgi-bin/kf/servicer/add?access_token=".$access_token;
        $data = array(
            'open_kfid'=>'wkPcnLGAAA2qLKuOq2TpGWw6IOwNn1UA',
            'userid_list'=>['youngtee']
        );
        $rs = juhecurl($url,json_encode($data),1);
        var_dump($rs);exit;
    }
}